package com.neusoft.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.neusoft.mapper.UserMapper;
import com.neusoft.po.User;
import com.neusoft.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserSeriveImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject getUserByIdByPass(User user) {
        User ret = userMapper.getUserByIdByPass(user);
        if (ret == null) {
            return null;
        }
        ret.setPassword(null);
        JSONObject jsonpObject = new JSONObject();
        String token = UUID.randomUUID().toString();
        userMapper.saveToken(user.getUserId(), token.replaceAll("-", ""));
        jsonpObject.put("token", token);
        jsonpObject.put("user", ret);
        return jsonpObject;
    }

    @Override
    public User getUserById(String userId) {
        return userMapper.getUserById(userId);
    }

    @Override
    public int saveUser(User user) {
        return userMapper.saveUser(user);
    }

    @Override
    public int updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

}
